/**
 * Created by hui on 16/7/22.
 */



/**
 * 卡号检查
 * @returns {boolean}
 */
function checkUserName() {
    var username = $("#username").val();
    if (username.length <= 0) {
        $('#tips').html('请输入卡号');
        return false;
    }
    if (username.length < 4) {
        $('#tips').html('卡号的长度不能小于4位');
        return false;
    }
    if (username.length > 20) {
        $('#tips').html('卡号的长度不能大于20位');
        return false;
    }
    return true;
}
/**
 * 密码检查
 * @returns {boolean}
 */
function checkPwd() {
    var password = $("#password").val();
    if (password.length <= 0) {
        $('#tips').html('请输入密码');
        return false;
    }
    if (password.length < 6) {
        $('#tips').html('密码的长度不能小于6位');
        return false;
    }
    if (password.length > 16) {
        $('#tips').html('密码的长度不能大于16位');
        return false;
    }
    return true;
}
/**
 * 验证码检查
 * @returns {boolean}
 */
/*function checkCaptcha() {
    var captcha = $("#captcha").val();
    if (captcha.length <= 0) {
        $('#tips').html('请输入验证码');
        return false;
    }
    return true;
}*/
/**
 * 获取验证码
 * @type {number}
 */
var id = new Date().getTime();
$(function () {
    $('#captcha').one('focus', (function () {
        $('#captchaImg').html('<img class="captcha" id="captchaCode" src="/captcha/' + id + '.html" height="40" width="100"/>');
    }));
});
$(function () {
    $('#reCaptcha').on('click', function () {
        $('#captchaImg').html('<img class="captcha" id="captchaCode" src="/captcha/' + id + '.html" height="40" width="100"/>');
    })
});
/**
 * 验证表单并提交
 */
$(function () {
    $('#submit').on('click', function submit() {
        var username = $("#username").val();
        var password = $("#password").val();
        var captcha = $("#captcha").val();
        var remember = $("#remember").prop('checked');
        var check = checkUserName() && checkPwd() ;//&& checkCaptcha();
        var path = window.location.pathname;
        var url = path == '/admin' || path == '/admin.html'?'admin.html':'index.html';
        if (check) {
            $.ajax({
                type: "POST",
                url: url,
                data: {
                    'username': username,
                    'password': password,
                    'captcha': captcha,
                    'remember': remember,
                    'id': id
                },
                success: function (msg) {
                    if (msg.status == 1) {
                        location.href = "center.html";
                    } else if (msg.status == 2) {
                        $("#tips").html('密码错误');
                        $("#reCaptcha").click();
                    } else if (msg.status == 3) {
                        $("#tips").html('卡号不存在');
                        $("#reCaptcha").click();
                    } else if (msg.status == 4) {
                        $("#tips").html('验证码错误');
                    }
                }
            })
        }
    });
});
/**
 * 绑定回车提交
 */
$(document).keypress(function (e) {
    if (e.which == 13) {
        $('#submit').click();
    }
});
